package com.stormrage.edu.ctrl.edu;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.stormrage.edu.serv.edu.EduMajorServ;
import com.stormrage.edu.serv.edu.EduProjectServ;
import com.stormrage.edu.serv.student.StuAcctServ;
import com.stormrage.entity.edu.EduMajor;
import com.stormrage.entity.edu.EduProject;
import com.stormrage.entity.edu.rep.EduProjectRep;
import com.stormragetech.core.ctrl.BaseCtrl;

/**
 * @Title: EduProjectCtrl.java
 * @Package com.stormrage.edu.ctrl.edu
 * @Description: 项目 Controller
 * @author www.stormragetech.com
 * @date 2016年4月17日
 * @version V1.0
 */
@Controller
@RequestMapping("/proj")
public class EduProjectCtrl extends BaseCtrl {

	private static final Logger logger = LoggerFactory.getLogger(EduProjectCtrl.class);

	@Autowired
	private EduProjectServ eduProjectServ;

	@Autowired
	private EduMajorServ eduMajorServ;

	@Autowired
	private StuAcctServ stuAcctServ;

	@RequestMapping("/")
	public String project(Model model) {

		logger.debug("【 Controller 】-【 IdxQandaCtrl 】-【 project 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户信息 ------------------
			Subject currentUser = SecurityUtils.getSubject();
			if (currentUser.getPrincipal() != null)
				model.addAttribute("user", stuAcctServ.queryByAcct(currentUser.getPrincipal().toString()));

			// ------------------ 获取用户操作的 Menu ------------------
			List<EduProject> menus = eduProjectServ.queryList();

			// ------------------ 获取Project List ------------------
			List<EduProjectRep> array = eduProjectServ.queryRepList();

			// ------------------ Model 赋值 ------------------
			model.addAttribute("menus", menus);
			model.addAttribute("array", array);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 IdxQandaCtrl 】-【 project 】-【 GET 】-【 COMPLETED 】");
		return "edu/index";

	}

	@RequestMapping("/{id}")
	public String project(@PathVariable("id") Integer id, Model model) {

		logger.debug("【 Controller 】-【 IdxQandaCtrl 】-【 List 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户信息 ------------------
			Subject currentUser = SecurityUtils.getSubject();
			if (currentUser.getPrincipal() != null)
				model.addAttribute("user", stuAcctServ.queryByAcct(currentUser.getPrincipal().toString()));

			// ------------------ 获取用户操作的 Menu ------------------
			List<EduProject> menus = eduProjectServ.queryList();

			// ------------------ 获取讲师List ------------------
			EduProject entity = eduProjectServ.queryById(id);
			List<EduMajor> array = eduMajorServ.queryListById(id);

			// ------------------ Model 赋值 ------------------
			model.addAttribute("menus", menus);
			model.addAttribute("data", entity);
			model.addAttribute("list", array);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		return "edu/project";
	}

}
